package com.shuaiche.yeguan.views.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.shuaiche.yeguan.R;
import com.shuaiche.yeguan.config.SCAppConfig;
import com.shuaiche.yeguan.ui.common.WebviewActivity;

import androidx.annotation.NonNull;

public class PermissionDialog extends Dialog {
    private Context context;
    private OnPermissionResultListener listener;

    private String title;
    private String content;

    public PermissionDialog(@NonNull Context context,String title,String content) {
        super(context, R.style.CustomDialog);
        this.context = context;
        this.title = title;
        this.content = content;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_permission);

        // 设置对话框宽度为屏幕宽度的80%
        Window window = getWindow();
        if (window != null) {
            WindowManager.LayoutParams params = window.getAttributes();
            params.width = (int) (getScreenWidth(context) * 0.8);
            window.setAttributes(params);
        }

        // 初始化视图
        TextView tvTitle = findViewById(R.id.tv_permission_title);
        TextView tvContent = findViewById(R.id.tv_permission_content);
        Button btnConfirm = findViewById(R.id.btn_apply);
        Button btnRefuse = findViewById(R.id.btn_reject);

        tvTitle.setText(title);
        tvContent.setText(content);

        // 设置点击事件
        btnConfirm.setOnClickListener(v -> {
            if (listener != null) {
                listener.onConfirm();
            }
            dismiss();
        });

        btnRefuse.setOnClickListener(v -> {
            if (listener != null) {
                listener.onRefuse();
            }
            dismiss();
        });
    }

    private int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(metrics);
        return metrics.widthPixels;
    }

    public void setOnPermissionResultListener(OnPermissionResultListener listener) {
        this.listener = listener;
    }

    public interface OnPermissionResultListener {
        void onConfirm();
        void onRefuse();
    }
}